package com.efounder.form.comp.multiple_images_selector;

import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.efounder.form.model.ImageModel;
import com.efounder.imageloader.GlideImageLoader;
import com.efounder.mobilecomps.R;
import com.pizidea.imagepicker.widget.ProgressView;

import java.util.List;

/**
 * Created by long on 2016/9/14.
 */

public class ViewAdapter<T> extends RecyclerView.Adapter<ViewAdapter.CustomViewHolder> {

//    ImageLoader imageLoader = ImageLoader.getInstance();
//    DisplayImageOptions options;
    Context mContext;
    List<T> listDatas;

    private int imageWidth;

    private int imageHeight;

    //是否显示加号
    private boolean isShowAddButton = true;

    public interface OnItemClickListener {
        void onItemClick(View view, int position);

        void onItemLongClick(View view, int position);
    }

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public ViewAdapter(Context context, List<T> datas) {
        this.mContext = context;
        this.listDatas = datas;

//        imageLoader.init(ImageLoaderConfiguration.createDefault(mContext));
//
//        options = new DisplayImageOptions.Builder()
//                .showImageOnLoading(R.drawable.default_img) //设置图片在下载期间显示的图片
//                .showImageForEmptyUri(R.drawable.default_img)//设置图片Uri为空或是错误的时候显示的图片
//                .showImageOnFail(R.drawable.default_img)  //设置图片加载/解码过程中错误时候显示的图片
//                .cacheInMemory(true)//设置下载的图片是否缓存在内存中
//                .cacheOnDisc(false)//设置下载的图片是否缓存在SD卡中
//                // .considerExifParams(true)  //是否考虑JPEG图像EXIF参数（旋转，翻转）
//                .imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2)//设置图片以如何的编码方式显示
//                .bitmapConfig(Bitmap.Config.RGB_565)//设置图片的解码类型//
////                .decodingOptions(android.graphics.BitmapFactory.Options)//设置图片的解码配置
////.delayBeforeLoading(int delayInMillis)//int delayInMillis为你设置的下载前的延迟时间
////设置图片加入缓存前，对bitmap进行设置
////.preProcessor(BitmapProcessor preProcessor)
//                // .resetViewBeforeLoading(true)//设置图片在下载前是否重置，复位
//                //  .displayer(new RoundedBitmapDisplayer(20))//是否设置为圆角，弧度为多少
//                .displayer(new FadeInBitmapDisplayer(0))//是否图片加载好后渐入的动画时间
//                .build();//构建完成
    }

    @Override
    public ViewAdapter.CustomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        CustomViewHolder holder = new CustomViewHolder(LayoutInflater.from(mContext).inflate(R
                .layout.recycler_item, parent, false));
        return holder;
    }

    @Override
    public void onBindViewHolder(final ViewAdapter.CustomViewHolder holder, int position) {
        //是否显示加号
        if (isShowAddButton) {
            if (position == listDatas.size()) {
                holder.imgView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
//                imageLoader.displayImage("drawable://" + R.drawable.icon_addpic_unfocused, holder.imgView);
                holder.imgView.setImageResource(R.drawable.icon_addpic_unfocused);
                holder.progressView.setVisibility(View.GONE);
            } else {

                holder.imgView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
                ImageModel data = (ImageModel) listDatas.get(position);

                String url = data.getUrl();
                if (url!=null&&!url.equals("")) {
                    GlideImageLoader.getInstance().displayImage(mContext,holder.imgView,url,R.drawable.default_img,R.drawable.default_img);
//                    imageLoader.displayImage(url, holder.imgView, options);
                }else{
//                    imageLoader.displayImage("drawable://" + R.drawable.icon_addpic_error, holder.imgView);
                    holder.imgView.setImageResource(R.drawable.icon_addpic_error);
                }

                int progress= data.getProgress();
                if (progress == 100 /*|| progress == 0*/) {
                    holder.progressView.setVisibility(View.GONE);
                } else {
                    holder.progressView.setVisibility(View.VISIBLE);
                    holder.progressView.setProgress(progress);
                }
            }
        } else {

            holder.imgView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
            ImageModel data = (ImageModel) listDatas.get(position);
            //EFRowSet data = (EFRowSet) listDatas.get(position);
            if (data.getUrl()!=null&&!data.getUrl().equals("")) {
                GlideImageLoader.getInstance().displayImage(mContext,holder.imgView,data.getUrl(),R.drawable.default_img,R.drawable.default_img);
//                imageLoader.displayImage(data.getUrl(), holder.imgView, options);
            }else{
//                imageLoader.displayImage("drawable://" + R.drawable.icon_addpic_error, holder.imgView);
                holder.imgView.setImageResource(R.drawable.icon_addpic_error);
            }
            holder.progressView.setVisibility(View.GONE);

        }
        if (null != onItemClickListener) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onItemClickListener.onItemClick(holder.itemView, holder.getLayoutPosition());
                }
            });

            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    onItemClickListener.onItemLongClick(holder.itemView, holder.getLayoutPosition
                            ());
                    return false;
                }
            });
        }

    }

    @Override
    public int getItemCount() {
        if (isShowAddButton)
            return listDatas.size() + 1;
        else
            return listDatas.size();
    }

    class CustomViewHolder extends RecyclerView.ViewHolder {
        ImageView imgView;
        ProgressView progressView;

        public CustomViewHolder(View itemView) {
            super(itemView);
            imgView = (ImageView) itemView.findViewById(R.id.item_grida_image);
            progressView = (ProgressView) itemView.findViewById(R.id.progressView);
            FrameLayout imgView_container = (FrameLayout) itemView.findViewById(R.id
                    .uploadimage_container);
            ViewGroup.LayoutParams params = imgView_container.getLayoutParams();
            params.height = getImageHeight();
            params.width = getImageWidth();
            imgView_container.setLayoutParams(params);

        }
    }

    public int getImageWidth() {
        return imageWidth;
    }

    public void setImageWidth(int imageWidth) {
        this.imageWidth = imageWidth;
    }

    public int getImageHeight() {
        return imageHeight;
    }

    public void setImageHeight(int imageHeight) {
        this.imageHeight = imageHeight;
    }

    public boolean isShowAddButton() {
        return isShowAddButton;
    }

    public void setShowAddButton(boolean showAddButton) {
        isShowAddButton = showAddButton;
    }

}
